<?php
class indexControl extends skymvc{
	
	public function __construct(){
		parent::__construct();
		 
	}
	
	public function onDefault(){
		if($_SERVER['HTTP_REFERER']=='' || preg_match("/m=login/",$_SERVER['HTTP_REFERER'])){
			$this->onMain();
		}else{
			$this->onList();
		}
	}
	public function onMain(){
		$admin=M("koudai_admin")->selectRow("adminid=".ADMINID);
		if($admin['typeid']==1){
			//今日订单
			$neworder=M("koudai_order")->selectOne(array(
				"where"=>" shopid=".SHOPID." AND status=0 ",
				"fields"=>" count(order_id) as c"
			));
			$dayorder=M("stat_shoporder_day")->selectRow("shopid=".SHOPID." AND k='koudai' AND kday='".date("Ymd")."' ");
			$monthorder=M("stat_shoporder_month")->selectRow("shopid=".SHOPID." AND k='koudai' AND kmonth='".date("Ym")."' ");
			$allmoney=M("stat_shoporder_month")->selectOne(array(
				"where"=>" shopid=".SHOPID." AND k='koudai' ",
				"fields"=>" sum(money) as money"
			));
		 
			$kucun=M("kucun")->selectRow("koudai=".SHOPID);
			if($kucun){
				$kcorder=M("stat_shoporder_day")->selectRow("shopid=".$kucun['koudai']." AND k='kcorder' AND kday='".date("Ymd")."' ");
			}
			$time=strtotime(date("Y-m-d"));
			$dznum=M("koudai_dz")->selectOne(array(
				"where"=>" shopid=".SHOPID." AND status=1 AND dateline>".$time."",
				"fields"=>" count(*) "
			));
			
			$undznum=M("koudai_dz")->selectOne(array(
				"where"=>" shopid=".SHOPID." AND status=3 AND dateline>".$time."",
				"fields"=>" count(*) "
			));
			
			$ssnum=M("koudai_order")->getOne("select count(*) from ".table('koudai_order')." WHERE shopid=".SHOPID." group by sc_id,sushe ");
			
			$stat_view_day=M("stat_view_day")->selectRow("shopid=".SHOPID." AND k='koudai' AND kday='".date("Ymd")."' ");
			
			$totalorder=M("koudai_order")->getOne("select count(*) from ".table('koudai_order')." WHERE shopid=".SHOPID." AND siteid=".SITEID." AND status in(0,1,2,3) ");
			$oids=M("koudai_order")->getCols("select order_id from ".table('koudai_order')." WHERE shopid=".SHOPID." AND status=3 AND isdz=0 ");
			$ykmoney=0;
			if($oids){
				$ykmoney=M("koudai_order_product")->getOne("select sum(amount*price) as ct from ".table('koudai_order_product')." WHERE order_id in("._implode($oids).") ");
			}
			$alluser=M("koudai_user")->selectOne(array(
				"where"=>" shopid=".SHOPID." ",
				"fields"=>" count(*) as ct "
			));
			$newuser=M("koudai_user")->selectOne(array(
				"where"=>" shopid=".SHOPID." AND kday='".date("Y-m-d")."' ",
				"fields"=>" count(*) as ct "
			));
			$this->smarty->assign(array(
				"neworder"=>$neworder,
				"dayorder"=>$dayorder,
				"kcorder"=>$kcorder,
				"dznum"=>$dznum,
				"undznum"=>$undznum,
				"sushe_num"=>$ssnum,
				"totalorder"=>$totalorder,
				"ykmoney"=>$ykmoney,
				"stat_view_day"=>$stat_view_day,
				"monthorder"=>$monthorder,
				"allmoney"=>$allmoney,
				"alluser"=>$alluser,
				"newuser"=>$newuser
			));
			$this->smarty->display("index.html");
		}else{
			
			//今日订单----------对账员
			$neworder=M("koudai_order")->selectOne(array(
				"where"=>" shopid=".SHOPID." AND status=0 ",
				"fields"=>" count(order_id) as c"
			));
			$dayorder=M("stat_shoporder_day")->selectRow("shopid=".SHOPID." AND k='koudai' AND kday='".date("Ymd")."' ");
			$monthorder=M("stat_shoporder_month")->selectRow("shopid=".SHOPID." AND k='koudai' AND kmonth='".date("Ym")."' ");
			$kucun=M("kucun")->selectRow("koudai=".SHOPID);
			if($kucun){
				$kcorder=M("stat_shoporder_day")->selectRow("shopid=".$kucun['koudai']." AND k='kcorder' AND kday='".date("Ymd")."' ");
			}
			$time=strtotime(date("Y-m-d"));
			$dznum=M("koudai_dz")->selectOne(array(
				"where"=>" shopid=".SHOPID." AND status=1 AND dateline>".$time."",
				"fields"=>" count(*) "
			));
			
			$dzmoney=M("koudai_dz")->selectOne(array(
				"where"=>" shopid=".SHOPID." AND status=1 AND dateline>".$time."",
				"fields"=>" sum(money) "
			));
			$income=$dzmoney*KD_DZ_MONEY;
			$undznum=M("koudai_dz")->selectOne(array(
				"where"=>" shopid=".SHOPID." AND status=3 AND dateline>".$time."",
				"fields"=>" count(*) "
			));
			
			$ssnum=M("koudai_order")->getOne("select count(*) from ".table('koudai_order')." WHERE shopid=".SHOPID." group by sc_id,sushe ");
			
			$stat_view_day=M("stat_view_day")->selectRow("shopid=".SHOPID." AND k='koudai' AND kday='".date("Ymd")."' ");
			
			$totalorder=M("koudai_order")->getOne("select count(*) from ".table('koudai_order')." WHERE shopid=".SHOPID." AND siteid=".SITEID." AND status in(0,1,2,3) ");
			$oids=M("koudai_order")->getCols("select order_id from ".table('koudai_order')." WHERE shopid=".SHOPID." AND status=3 AND isdz=0 ");
			$ykmoney=0;
			if($oids){
				$ykmoney=M("koudai_order_product")->getOne("select sum(amount*price) as ct from ".table('koudai_order_product')." WHERE order_id in("._implode($oids).") ");
			}
			
			$this->smarty->assign(array(
				"neworder"=>$neworder,
				"dayorder"=>$dayorder,
				"kcorder"=>$kcorder,
				"dznum"=>$dznum,
				"dzmoney"=>$dzmoney,
				"undznum"=>$undznum,
				"income"=>$income,
				"sushe_num"=>$ssnum,
				"totalorder"=>$totalorder,
				"ykmoney"=>$ykmoney,
				"stat_view_day"=>$stat_view_day,
				"monthorder"=>$monthorder
			));
			$this->smarty->display("index_dz.html");
		}
		
	}
	
	public function onList(){
		$admin=M("koudai_admin")->selectRow("adminid=".ADMINID);
		if($admin['typeid']==1){
			$this->smarty->display("list.html");
		}else{
			$this->smarty->display("list_dz.html");
		}
	}
	
	
	public function onweicang(){
		$admin=M("koudai_admin")->selectRow("adminid=".ADMINID);
		$this->smarty->display("weicang.html");
	}
}

 
?>